<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2019/5/9
 * Time: 10:50
 */

namespace app\api2\Service;


use think\Db;

class DemandService
{
    /**
     * 退回某个需求的保证金
     * @param $demand_id 需求ID
     * @param bool $invalid 是否让订单无效
     */
    static function back_money($demand_id,$invalid=false){
        Db::transaction(function () use ($demand_id,$invalid){
            Db::name('member_demand')
                ->where('id',$demand_id)
                ->where('is_back_money',0)
                ->setField('is_back_money',1);
            $db=Db::name('demand_offer')
                ->where('demand_id',$demand_id)
                ->where('is_back_money',0)
                ->field('offer_money,mid');
            $demand_offer=$db->select();
            foreach ($demand_offer as $v){
                //退款
                if($v['offer_money']>0){
                    log_account_change($v['mid'],$v['offer_money'],0,'竞标需求失败，退回保证金',4);
                }
            }
            Db::name('demand_offer')
                ->where('demand_id',$demand_id)
                ->where('is_back_money',0)
                ->setField('is_back_money',1);
            $db=Db::name('demand_offer')
                ->where('demand_id',$demand_id);
            if(!$invalid){
                $db->where('status',1);
            }
            $db->setField('status',0);
        });
    }
}